package com.xr.system.common.config.mybatisPlusConfig;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

import java.util.List;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

public class LogicDeleteWithFill extends AbstractMethod {

	/**
	 * mapper对应的方法名
	 */
	private static final String MAPPER_METHOD = "deleteWithFill";

	private static final long serialVersionUID = -402373469518227818L;

	@Override
	public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
		String sql;
		SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
		if (tableInfo.isWithLogicDelete()) {
			List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream()
					.filter(i -> i.getFieldFill() == FieldFill.UPDATE || i.getFieldFill() == FieldFill.INSERT_UPDATE)
					.collect(toList());
			if (CollectionUtils.isNotEmpty(fieldInfos)) {
				String sqlSet = "SET " + fieldInfos.stream().map(i -> i.getSqlSet(ENTITY_DOT)).collect(joining(EMPTY))
						+ tableInfo.getLogicDeleteSql(false, false);
				sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet,
						sqlWhereEntityWrapper(true, tableInfo), sqlComment());
			}
			else {
				sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo),
						sqlWhereEntityWrapper(true, tableInfo), sqlComment());
			}
			SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
			return addUpdateMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource);
		}
		else {
			sqlMethod = SqlMethod.DELETE;
			sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),
					sqlComment());
			SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
			return addDeleteMappedStatement(mapperClass, MAPPER_METHOD, sqlSource);
		}
	}

}
